package org.linlinjava.litemall.core.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.reflection.MetaObject;

import java.time.LocalDateTime;
import java.util.Objects;

public class MyMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        LocalDateTime date = LocalDateTime.now();
        if(!StringUtils.isEmpty(metaObject.findProperty("addTime", false))
            && Objects.isNull(this.getFieldValByName("addTime", metaObject))){
            setFieldValByName("addTime", date, metaObject);
        }

        if(!StringUtils.isEmpty(metaObject.findProperty("updateTime", false))
                && Objects.isNull(this.getFieldValByName("updateTime", metaObject))){
            setFieldValByName("updateTime", date, metaObject);
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {

    }
}
